/**
 * 商品编辑
 * */


var catelist = [];


(function () {
})();


//获取一个
function getOne(){
    $.ajax({
        type: apis.jfGone.method,
        url: apis.jfGone.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                return
            }
            $('#load').html('');
            var d = res.data;
            formData.title = d.title;
            formData.cateid = d.cate_id;
            formData.bigImg = d.img;
            formData.isnew = d.isnew;
            moreImgs = d.moreImgs;
            $('#title')[0].value = d.title;
            var h = '', i;
            //分类
            if (catelist.length > 0) {
                for (i = 0; i < catelist.length; i ++) {
                    if (catelist[i].id == formData.cateid) {
                        h += '<option value="' + catelist[i].id + '" selected>';
                    } else {
                        h += '<option value="' + catelist[i].id + '">';
                    }
                    h += catelist[i].title;
                    h += '</option>';
                }
                $('#cateid').html(h);
            }
            //主图
            if (d.img != '') {
                h = '<img src="' + d.imgDomain + d.img + '" style="width: ' + d.imgw + 'px; height: ' + d.imgh + 'px;"/>';
                $('#bigShow').html(h).width(d.imgw).height(d.imgh);
            }
            //多图
            if (d.moreImgs.length > 0) {
                h = '';
                var outW = 0,outH = 0;
                for (i = 0; i < moreImgs.length; i ++) {
                    h += '<div style="width: ' + (moreImgs[i].w*1+2)+ 'px; height: ' + moreImgs[i].h + 'px; overflow: hidden;">';
                    h += '<img src="' + moreImgs[i].imgDomain + moreImgs[i].img + '" style="width: ' + moreImgs[i].w + 'px; height: ' + moreImgs[i].h + 'px; border: 1px solid #000;"/>';
                    h += '<a href="javascript:;" style="padding: 10px 20px; color: #fff; background: red; font-size: 12px; opacity: 0.7; position: relative; top: -30px; left: 0;" onclick="imgDel(' + moreImgs[i].id + ');">点击删除</a>';
                    h += '</div>';
                    if (outW < moreImgs[i].w) { outW = moreImgs[i].w; }
                    if (outH < moreImgs[i].h) { outH = moreImgs[i].h; }
                }
                $('#moreOut').width(outW);
                $('#moreTotal').html('当前' + moreImgs.length + '张');
                $('#moreShow').html(h).width(outW*1+2).height(outH*1+2);
            }
            //上新
            h = '';
            h += '<div class="radio-box" onclick="formData.isnew=0;">';
            if (d.isnew == 0) {
                h += '<input name="isnew" type="radio" id="sex-1" value="0" checked/>';
            } else {
                h += '<input name="isnew" type="radio" id="sex-1" value="0"/>';
            }
            h += '<label for="sex-1">否</label>';
            h += '</div>';
            h += '<div class="radio-box" onclick="formData.isnew=1;">';
            if (d.isnew == 1) {
                h += '<input name="isnew" type="radio" id="sex-2" value="1" checked/>';
            } else {
                h += '<input name="isnew" type="radio" id="sex-2" value="1"/>';
            }
            h += '<label for="sex-2">是</label>';
            h += '</div>';
            $('#newOut').html(h);
            //SKU
            var skus = d.skuLists;
            if (skus.length > 0) {
                 h = '';
                for (i = 0; i < skus.length; i ++) {
                     h += '<div class="skuOne">';
                     h += '<div class="row cl">';
                     h += '<label class="form-label col-xs-4 col-sm-2">规格名称：</label>';
                     h += '<div class="formControls col-xs-8 col-sm-9">';
                     h += '<input type="text" class="input-text" id="sku_title_' + skus[i].id + '" value="' + skus[i].title + '"/>';
                     h += '</div>';
                     h += '</div>';
                     h += '<div class="row cl">';
                     h += '<label class="form-label col-xs-4 col-sm-2">积分数：</label>';
                     h += '<div class="formControls col-xs-8 col-sm-9">';
                     h += '<input type="text" class="input-text" id="sku_price_' + skus[i].id + '" value="' + skus[i].price + '"/>';
                     h += '</div>';
                     h += '</div>';
                     h += '<div class="row cl">';
                     h += '<label class="form-label col-xs-4 col-sm-2">划线价(元)：</label>';
                     h += '<div class="formControls col-xs-8 col-sm-9">';
                     h += '<input type="text" class="input-text" id="sku_price_line_' + skus[i].id + '" value="' + skus[i].price_line + '"/>';
                     h += '</div>';
                     h += '</div>';
                     h += '<div class="row cl">';
                     h += '<label class="form-label col-xs-4 col-sm-2">库存量：</label>';
                     h += '<div class="formControls col-xs-8 col-sm-9">';
                     h += '<input type="text" class="input-text" id="sku_stock_' + skus[i].id + '" value="' + skus[i].stock + '"/>';
                     h += '</div>';
                     h += '</div>';
                     h += '<div class="row cl">';
                     h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
                     h += '<button class="btn btn-primary radius" type="button" onclick="skuModify(' + skus[i].id + ');">';
                     h += '<i class="Hui-iconfont">&#xe632;</i> ';
                     h += '确定修改';
                     h += '</button>';
                     h += ' &nbsp; ';
                     h += '<button class="btn btn-primary radius" type="button" style="background: red; border: 0;" onclick="skuDel(' + skus[i].id + ');">';
                     h += '删除该SKU';
                     h += '</button>';
                     h += '</div>';
                     h += '</div>';
                     h += '</div>';
                     h += '';
                }
                $('#skuGroup').html(h);
            }
            //详情
            ue.setContent(d.detail);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//获取分类
function getCateList(){
    $.ajax({
        type: apis.jfGcateSel.method,
        url: apis.jfGcateSel.url,
        data: {},
        success: function (res) {
            //console.log(res);
            if (res.code < 0) {
                return
            }
            catelist = res.data;
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//抓图
function uploadMore() {
    console.log(moreImgs);
    if (moreImgs.length >= 5) {
        layer.msg('详情图最多5张',{icon: 0,time: 1000});
        return
    }
    var fileObj = $('#moreUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        //console.log(e.target.result);
        $.ajax({
            type: apis.jfGmoreUpload.method,
            url: apis.jfGmoreUpload.url,
            data: { id: id, img: e.target.result },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layerMsgErr(res.msg);
                    return
                }
                layerMsgOk('详情图上传成功');
                setTimeout(function () {
                    location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}


//显示图片
function showImg(e){
    var selid = e.type==1 ? 'big' : 'more';
    var domain = e.domain ? e.domain : '';
    var h = '', i;
    h += '<img src="' + domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    //h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#' + selid + 'Show').html(h).height(e.h);
    if (e.type == 1) {
        formData.bigImg = e.img;
    }
}


//移除图片
function imgDel(v){
    $.ajax({
        type: apis.jfGmoreDel.method,
        url: apis.jfGmoreDel.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg);
                return
            }
            layerMsgOk('详情图删除成功');
            setTimeout(function () {
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//保存商品
function gSave(){
    if (!formData.title) {
        layer.msg('商品名称必须的',{icon: 0,time: 1000});
        return
    }
    if (!formData.cateid) {
        layer.msg('分类必须的',{icon: 0,time: 1000});
        return
    }
    if (formData.bigImg == '') {
        layer.msg('主图必须的',{icon: 0,time: 1000});
        return
    }
    formData.id = id;
    formData.detail = ue.getContent();
    //console.log(formData); return
    $.ajax({
        type: apis.jfGmodify.method,
        url: apis.jfGmodify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '商品修改成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//SKU复位
function skuInit(){
    $('#sku_title')[0].value = '';
    $('#sku_price')[0].value = 0;
    $('#sku_price_line')[0].value = 0;
    $('#sku_stock')[0].value = 0;
}


//添加SKU
function skuAdd(){
    var skuData = {
        gid: id,
        title: $('#sku_title').val(),
        price: $('#sku_price').val(),
        price_line: $('#sku_price_line').val(),
        stock: $('#sku_stock').val(),
    };
    if (skuData.title == '') {
        layerMsgErr('规格名称必须的');
        return
    }
    if (skuData.price == '') {
        layerMsgErr('积分数必须的');
        return
    }
    if (skuData.price_line == '') {
        layerMsgErr('划线价必须的');
        return
    }
    if (skuData.stock == '') {
        layerMsgErr('库存必须的');
        return
    }
    //console.log(skuData); return
    $.ajax({
        type: apis.jfGskuAdd.method,
        url: apis.jfGskuAdd.url,
        data: skuData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: 'SKU添加成功'});
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//保存SKU
function skuModify(v){
    var skuData = {
        id: v,
        title: $('#sku_title_' + v).val(),
        price: $('#sku_price_' + v).val(),
        price_line: $('#sku_price_line_' + v).val(),
        stock: $('#sku_stock_' + v).val(),
    };
    if (skuData.title == '') {
        layerMsgErr('规格名称必须的');
        return
    }
    if (skuData.price == '') {
        layerMsgErr('单价必须的');
        return
    }
    if (skuData.price_line == '') {
        layerMsgErr('划线价必须的');
        return
    }
    if (skuData.stock == '') {
        layerMsgErr('库存必须的');
        return
    }
    //console.log(skuData); return
    $.ajax({
        type: apis.jfGskuModify.method,
        url: apis.jfGskuModify.url,
        data: skuData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            //msgPop({msg: 'SKU修改成功'});
            layerMsgOk('SKU修改成功');
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//移除SKU
function skuDel(v) {
    $.ajax({
        type: apis.jfGskuDel.method,
        url: apis.jfGskuDel.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layerMsgErr(res.msg); return
            }
            //msgPop({msg: 'SKU删除成功'});
            layerMsgOk('SKU删除成功');
            setTimeout(function(){
                //top.location.reload();
                location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}